package org.springblade.tms.module.supercargoinfo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.tms.global.entity.TimeInterval;
import org.springblade.tms.global.enums.ApprovalStatusEnum;
import org.springblade.tms.module.carriersupercargorelation.entity.TMCarrierSupercargoRelation;
import org.springblade.tms.module.carriersupercargorelation.service.ICarrierSupercargoRelationService;
import org.springblade.tms.module.common.dto.DeleteDto;
import org.springblade.tms.module.supercargoinfo.base.EscortManagementService;
import org.springblade.tms.module.supercargoinfo.entity.TMSupercargoInfo;
import org.springblade.tms.module.supercargoinfo.importer.EscortManagementImporter;
import org.springblade.tms.module.supercargoinfo.vo.TMSupercargoInfoExcelVo;
import org.springblade.tms.module.supercargoinfo.vo.TMSupercargoInfoVO;
import org.springblade.tms.module.userinfo.base.DriverManagementService;
import org.springblade.tms.module.userinfo.dto.CarrierAuditReq;
import org.springblade.tms.module.userinfo.entity.TMUserInfo;
import org.springblade.tms.module.userinfo.service.IUserInfoService;
import org.springblade.tms.module.userinfo.vo.QualificationReviewCarrier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.supercargoinfo.controller
 * @className: EscortManagementController
 * @author: yr
 * @description: 押运员
 * @date: 2022/3/10 15:36
 */
@RestController
@AllArgsConstructor
@RequestMapping("supercargoinfo/EscortManagementController")
@Api(value = "押运员管理-yr", tags = "押运员管理接口-yr")
public class EscortManagementController {
	@Resource
	private final EscortManagementService escortManagementService;
	@Resource
	private ThreadPoolExecutor dynamicTp;
	@Resource
	private Validator validator;
	@Resource
	private RedisTemplate redisTemplate;
	@Resource
	private DriverManagementService driverManagementService;
	@Resource
	private ICarrierSupercargoRelationService carrierSupercargoRelationService;
	@Resource
	private IUserInfoService userInfoService;

	/**
	 * @param tmSupercargoInfo :
	 * @param query            :
	 * @return R<IPage < TMUserInfoVO>>
	 * @author yr
	 * @description 押运员信息查询
	 * @date 2022/3/11
	 */
	@ApiLog("押运员信息查询")
	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "押运员信息查询", notes = "传入tmSupercargoInfo")
	public R<IPage> list(TMSupercargoInfoVO tmSupercargoInfo, Query query, TimeInterval timeInterval) {
		return escortManagementService.list(tmSupercargoInfo, query, timeInterval);

	}

	/**
	 * @param tmSupercargoInfo:
	 * @author yr
	 * @description 新增或修改押运员
	 * @date 2022/3/10 15:44
	 */
	@ApiLog("新增或修改押运员")
	@PostMapping("/saveOrUpdate")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "新增或修改押运员", notes = "传入tmSupercargoInfo")
	public R saveOrUpdate(@Valid @RequestBody TMSupercargoInfo tmSupercargoInfo) {
		return escortManagementService.saveOrUpdate(tmSupercargoInfo, driverManagementService.getTmUserInfo());
	}

	/**
	 * @return
	 * @Desc 押运员审核
	 * @author yr
	 * @Date 2022/3/7
	 */
	@ApiLog("押运员审核")
	@PostMapping("/check")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "押运员审核", notes = "传入carrierAuditReq")
	public R check(@RequestBody List<CarrierAuditReq> carrierAuditReq) {
		return escortManagementService.check(carrierAuditReq);
	}

	/**
	 * @param tmSupercargoInfo :
	 * @param query            :
	 * @return R<IPage < TMUserInfoVO>>
	 * @author yr
	 * @description 资质审核查询
	 * @date 2022/3/11
	 */
	@ApiLog("资质审核查询")
	@GetMapping("/qualificationReview")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "资质审核查询", notes = "传入tmSupercargoInfo")
	public R<IPage> qualificationReview(TMSupercargoInfoVO tmSupercargoInfo, Query query, TimeInterval timeInterval) {
		tmSupercargoInfo.setApprovalStatus(ApprovalStatusEnum.PENDING_REVIEW.getValue());
		return escortManagementService.list(tmSupercargoInfo, query, timeInterval);

	}

	/**
	 * @param deleteDto:
	 * @return R
	 * @author yr
	 * @description 批量删除押运员
	 * @date 2022/3/11 10:16
	 */
	@ApiLog("删除押运员")
	@PostMapping("/delete")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "批量删除押运员", notes = "传入ids")
	public R delete(@RequestBody DeleteDto deleteDto) {
		return escortManagementService.delete(deleteDto.getIds());
	}


	@ApiLog("导入Excel")
	@PostMapping("/importIntoExcel")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "导入Excel", notes = "传入file")
	public R importIntoExcel(@Valid MultipartFile file) {
		String importNo = UUID.randomUUID().toString();
		ExcelUtil.save(file, new EscortManagementImporter(escortManagementService, dynamicTp, validator, driverManagementService.getTmUserInfo(), importNo, redisTemplate), TMSupercargoInfoExcelVo.class);
		Object num = redisTemplate.opsForValue().get(importNo);
		redisTemplate.delete(importNo);
		return R.data(num);
	}

	@ApiLog("押运员数据模板")
	@GetMapping("/export")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "押运员数据模板")
	public void exportNotice(HttpServletResponse response) {
		List<TMSupercargoInfoExcelVo> list = new ArrayList<>();
		TMSupercargoInfoExcelVo tmSupercargoInfoExcelVo = new TMSupercargoInfoExcelVo();
		tmSupercargoInfoExcelVo.setCertificateWorkNo("string");
		list.add(tmSupercargoInfoExcelVo);
		ExcelUtil.export(response, "押运员数据模板", "押运员数据模板", list, TMSupercargoInfoExcelVo.class);
	}

	/**
	 * @param id:
	 * @return R<IPage>
	 * @author yr
	 * @description 车辆资质审查列表
	 * @date 2022/3/9 11:48
	 */
	@ApiLog("押运员资质审查承运商列表")
	@GetMapping("/qualificationReviewCarrierList")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "押运员资质审查承运商列表", notes = "传入id")
	public R<List<QualificationReviewCarrier>> qualificationReviewCarrierList(Long id) {
		List<TMCarrierSupercargoRelation> tmCarrierCarRelations = carrierSupercargoRelationService.lambdaQuery().eq(TMCarrierSupercargoRelation::getRelationSupercargoNo, id).list();
		List<Long> carrierIds = tmCarrierCarRelations.stream().map(TMCarrierSupercargoRelation::getRelationCarrierNo).collect(Collectors.toList());
		List<TMUserInfo> tmUserInfos = userInfoService.lambdaQuery()
			.in(!carrierIds.isEmpty(), TMUserInfo::getId, carrierIds)
			.apply(carrierIds.isEmpty(), "1=2")
			.list();
		return R.data(BeanUtil.copy(tmUserInfos, QualificationReviewCarrier.class));
	}
}
